<template>
	<view class="container-list">
		<!-- 头部导航栏 -->
		<view class="head">
			<view class="headbtn" @click="change('0')">
				全部
				<view class="btn" :class="{btna:btnnum == '0'}"></view>
			</view>
			<view class="headbtn" @click="change('1')">
				待付款
				<view class="btn" :class="{btna:btnnum == '1'}"></view>
			</view>
			<view class="headbtn" @click="change('2')">
				待发货
				<view class="btn" :class="{btna:btnnum == '2'}"></view>
			</view>
			<view class="headbtn" @click="change('3')">
				待收货
				<view class="btn" :class="{btna:btnnum == '3'}"></view>
			</view>
			<view class="headbtn" @click="change('4')">
				已完成
				<view class="btn" :class="{btna:btnnum == '4'}"></view>
			</view>
			<view class="headbtn" @click="change('5')">
				售后
				<view class="btn" :class="{btna:btnnum == '5'}"></view>
			</view>
		</view>
		<!-- 列表区域 -->
		<view class="order-list" v-if="show_lists.length">
			<!-- 订单组件 -->
			<my-orderlist :orderList="show_lists" @cancle='cancleOrder' @updateList="updateList" @failCancle="cancleFailed"></my-orderlist>
		</view>

		<view class="no-data" v-else>
			<image src="/static/icons/shopping_empty@3x.png" class="empty-cart"></image>
			<view class="tips">
				您还没有相关的订单
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				btnnum: '0',
				orderList: [], // 订单列表
				show_lists: [], // 展示订单列表
				state: ''
			};
		},
		onShow() {
			this.getAllOrderList(this.state)
		},
		onLoad() {
			
			uni.setStorage({
				key: 'page',
				data: 'order-list'
			});

			// 如果是从个人中心页点击有目标的订单
			if (uni.getStorageSync('state') !== '') {
				console.log('state')
				this.state = uni.getStorageSync('state')
				this.btnnum = uni.getStorageSync('btnNum')
				this.getAllOrderList(this.state)

				this.show_lists = []
				this.orderList.forEach(item => {
					if (item.state == this.btnnum) {
						this.show_lists.push(item)
					}
				})
				// 获取所有的订单
			} else {
				this.state = ''
				this.btnnum = '0'
				this.getAllOrderList(this.state)
			}
			// this.getAllOrderList(this.state)

			// if (uni.getStorageSync('btnNum')) {
			// 	this.btnnum = uni.getStorageSync('btnNum')
			// }
		},
		methods: {
			updateList(){
				this.state='0'
				this.btnnum='0'
				this.getAllOrderList(this.state)
			},
			// 取消订单
			cancleOrder() {
				this.state='0'
				this.btnnum='0'
				this.getAllOrderList(this.state)
				uni.$showMsg('订单取消成功！')
			},
			// 取消订单失败
			cancleFailed(){
				this.getAllOrderList(this.state)
				uni.$showMsg('订单取消失败！')
			},
			// ...mapMutations('m_order',['saveOrderToStorage']),
			// 获取所有的订单列表
			async getAllOrderList(state) {
				// console.log(this.userInfo._id)
				const {
					data: res
				} = await uni.$http.post('/order/getOrderList', {
					userId: this.userInfo._id,
					state: state,
					offset: 1,
					limit: 99
				})
				this.orderList = res.data.list
				// this.saveOrderToStorage(res.data.list)
				// console.log(this.orderList)
				this.show_lists = this.orderList
			},
			change(e) {
				this.btnnum = e
				if (e != '0') {
					this.state = e
				} else {
					this.state = ''
				}
				console.log(this.btnnum)
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 300
				});

				this.getAllOrderList(this.state)

				this.show_lists = []
				this.orderList.forEach(item => {
					if (item.state == this.btnnum) {
						this.show_lists.push(item)
					}
				})

				if (this.btnnum == '0') {
					this.show_lists = this.orderList
					console.log(this.show_lists)
				} else {
					// 待付款订单列表
					this.show_lists = []
					this.orderList.forEach(item => {
						if (item.state == this.btnnum) {
							this.show_lists.push(item)
						}
					})
				}
			}
		},
		computed: {
			...mapState('m_user', ['userInfo']),
			// ...mapState('m_order', ['order'])
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f4f4f4;
	}

	.head {
		display: flex;
		justify-content: space-evenly;
		position: sticky;
		top: 0;
		background-color: #fff;
		padding-bottom: 10rpx;
		box-shadow: 1px 2px 9px 1px rgba(0, 0, 0, 0.17);

		.headbtn {
			flex: 1;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			position: relative;
		}
	}

	.btna {
		position: absolute;
		bottom: 0;
		width: 66rpx;
		height: 6rpx;
		left: 50%;
		transform: translateX(-50%);
		background-color: #D80E0E;
	}

	.order-list {
		padding-bottom: 50rpx;
	}

	.no-data {
		display: flex;
		flex-direction: column;

		.empty-cart {
			width: 280rpx;
			height: 260rpx;
			margin: auto;
			margin-top: 200rpx;
		}

		.tips {
			text-align: center;
			margin-top: 100rpx;
			color: #999;
		}
	}
</style>
